using System;
using System.Collections.Generic;

namespace Model.Common
{
    /// <summary>
    /// 统一的API返回结果
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class ApiResult<T>
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ApiResult()
        {
            Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
        }

        /// <summary>
        /// 成功返回
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public static ApiResult<T> SuccessResult(T data, string message = "操作成功")
        {
            return new ApiResult<T>
            {
                Success = true,
                Code = 200,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 失败返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        public static ApiResult<T> ErrorResult(string message = "操作失败", int code = 500)
        {
            return new ApiResult<T>
            {
                Success = false,
                Code = code,
                Message = message,
                Data = default(T)
            };
        }

        /// <summary>
        /// 参数错误返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult<T> BadRequestResult(string message = "参数错误")
        {
            return new ApiResult<T>
            {
                Success = false,
                Code = 400,
                Message = message,
                Data = default(T)
            };
        }

        /// <summary>
        /// 未授权返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult<T> UnauthorizedResult(string message = "未授权")
        {
            return new ApiResult<T>
            {
                Success = false,
                Code = 401,
                Message = message,
                Data = default(T)
            };
        }

        /// <summary>
        /// 未找到返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult<T> NotFoundResult(string message = "未找到")
        {
            return new ApiResult<T>
            {
                Success = false,
                Code = 404,
                Message = message,
                Data = default(T)
            };
        }
    }

    /// <summary>
    /// 无数据的API返回结果
    /// </summary>
    public class ApiResult : ApiResult<object>
    {
        /// <summary>
        /// 成功返回
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns></returns>
        public static ApiResult SuccessResult(string message = "操作成功")
        {
            return new ApiResult
            {
                Success = true,
                Code = 200,
                Message = message,
                Data = null
            };
        }

        /// <summary>
        /// 失败返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        public static ApiResult ErrorResult(string message = "操作失败", int code = 500)
        {
            return new ApiResult
            {
                Success = false,
                Code = code,
                Message = message,
                Data = null
            };
        }

        /// <summary>
        /// 参数错误返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult BadRequestResult(string message = "参数错误")
        {
            return new ApiResult
            {
                Success = false,
                Code = 400,
                Message = message,
                Data = null
            };
        }

        /// <summary>
        /// 未授权返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult UnauthorizedResult(string message = "未授权")
        {
            return new ApiResult
            {
                Success = false,
                Code = 401,
                Message = message,
                Data = null
            };
        }

        /// <summary>
        /// 未找到返回
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        public static ApiResult NotFoundResult(string message = "未找到")
        {
            return new ApiResult
            {
                Success = false,
                Code = 404,
                Message = message,
                Data = null
            };
        }
    }

    /// <summary>
    /// 分页结果
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class PagedResult<T>
    {
        /// <summary>
        /// 数据列表
        /// </summary>
        public List<T> Items { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPreviousPage => PageIndex > 1;

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNextPage => PageIndex < TotalPages;
    }
}
